#include "Ship.h"
#include <fstream>
#include <iostream>
#include "../Games/MyGame.h"
#include "../Scene/SceneNode.h"

Ship::Ship(int maxFrequency, std::string name) :
		GameEntity(maxFrequency, name)
{
	_directionState = NONE;	
}

Ship::~Ship()
{
}

void Ship::draw()
{	
	SCALE scale = {0.5,0.5,0.5};
	GameEngine::Instance()->getRenderer()->drawPieceQuad("assets/spaceship3.raw\0", 1,1,1, this->getPosition(),this->getOrientation(), scale, 128,128,getColor());
}

void Ship::actualUpdate(unsigned long long timePassed)
{
	POINT3D newPosition = {getPosition().x, getPosition().y, getPosition().z};

	switch(_directionState) {
	case LEFT: 
		newPosition.x -= 0.7f;
		break;
	case RIGHT: 
		newPosition.x += 0.7f;
		break;
	case UP: 
		newPosition.y += 0.7f;
		break;
	case DOWN: 
		newPosition.y -= 0.7f;
		break;
	}
	if(MyGame::Instance()->getEnv()->isLegalLocation(newPosition))
		setPosition(newPosition);

	//_directionState = NONE;

}
void Ship::onMouseScrolled(int xcoord, int ycoord, int distance){
}

void Ship::reset(){}

void Ship::onKeyPressed(int key){
	switch(key)
	{	
		case 'a':case 'A':	_directionState = LEFT;
					break;
		case 'd':case 'D':	_directionState = RIGHT;
					break;
		case 'w':case 'W':	_directionState = UP;
					break;
		case 's':case 'S':	_directionState = DOWN;
					break;
		case 'f':case 'F': {
			if(this->getMySceneNode()->getChildren().size() <= 2)
				createShot();
		}
		break;
	}
}			//If a key is pressed, this function is called
void Ship::onKeyReleased(int key){
	switch(key)
	{
		case 'a':case 'A':	{
			if(_directionState == LEFT)
				_directionState = NONE;
			break;
		}
		case 'd':case 'D':	{
			if(_directionState == RIGHT)
				_directionState = NONE;
			break;
		}
		case 'w':case 'W':	{
			if(_directionState == UP)
				_directionState = NONE;
			break;
		}
		case 's':case 'S':	{
			if(_directionState == DOWN)
				_directionState = NONE;
			break;
		}
	}
}		//If a key is released, this function is called

void Ship::onMouseMoved(int, int){}			//If a mouseIsMoved, this function is called
void Ship::onMouseClicked(int, int){}			//If a mouseIsClicked, this function is called

int Ship::getType() {
	return SHIP;
}

void Ship::createShot() {

	POINT3D shipPOS = this->getPosition();
	ORIENTATION shipOR = this->getOrientation();
	//shipPOS.y += 2.5f;
	//shipPOS.x += 1.5f;
	MyGame::Instance()->createEntity(this->getMySceneNode(),0,SHIPSHOT,40,"shipshot",shipOR,shipPOS,this->getColor());
}